import os
import pandas as pd
import numpy as np

def test1():
    df = pd.DataFrame([('bird', 'Falconiformes', 389.0),
                       ('bird', 'Psittaciformes', 24.0),
                       ('mammal', 'Carnivora', 80.2),
                       ('mammal', 'Primates', np.nan),
                       ('mammal', 'Carnivora', 58)],
                      index=['falcon', 'parrot', 'lion', 'monkey', 'leopard'],
                      columns=('class', 'order', 'max_speed'))
    print(df)
    print()
    # grouped = df.groupby('class').max()
    # print(grouped)
    grouped = df.groupby("class").agg(lambda x:[].append(x))
    print(grouped)

def test2():
    df = pd.DataFrame({'uid': [1, 2, 3, 4],
                       'B': [1, 2, 3, 4],
                       'C': [4, 5, 6, 7]})

    df2 = pd.DataFrame({'uid': [1, 1, 2, 2],
                        'hello': ['context', 'c2', 'c3', 'c4'],
                        'log': [4, 3, 2, 1]
                        })
    print(df)
    print(df2)
    print()
    df2 = df2.drop_duplicates()
    print(df2)

    print("### group by")
    # print(df2.groupby('uid')['log'])
    df_x = df2.groupby('uid').apply(lambda x: x.sort_values('log', ascending=True))
    print(df_x, type(df_x))

    df3 = df_x.groupby('uid')["log"].apply(lambda x: "_".join([str(y) for y in x] ))
    print("### df3")
    print(df3)
    print()
    # df['bag_twts'] = df.uid.map(df3)

    # print(df)

def test3():
    df = pd.DataFrame({'uid': [1, 2, 2, 4],
                       'B': [1, 2, 3, 4],
                       'C': [4, 5, 6, 7]})
    for key in df.keys():
        print("key:{}  count".format(key))
        print(df.loc[:, key].value_counts())
        print("---")

def test4():
    df1 = pd.DataFrame({'uid': [1, 2, 3, 4],
                       'cid': [1, 2, 3, 4]})
    df2 = pd.DataFrame({'time':[1, 2, 5, 4, 6, 100, 99],
                        'cid':[1, 1, 2, 2, 3, 4, 4]})
    df3 = pd.merge(df2, df1, on="cid", how="left")
    print(df3.groupby("uid")["time"])
    df4 = df3.groupby("uid").apply(lambda x: x.sort_values('time', ascending=True))
    print("df4", df4)

    df5 = df4.groupby("uid")["cid"].apply(list).reset_index()
    print(df5)

def test5():
    df1 = pd.DataFrame({'uid': [1, 2, 3, 4],
                       'cid': [1, 2, 3, 4]})
    df2 = pd.DataFrame({'time':[1, 2, 5, 4, 6, 100, 99],
                        'cid':[1, 1, 2, 2, 3, 4, 4]})
    df1 = df1.rename_axis(None)
    df2 = df2.rename_axis(None)
    df3 = pd.merge(df2, df1, on="cid", how="left")
    df3 = df3.rename_axis(None)
    df4 = df3.groupby("uid").apply(lambda x: x.sort_values('time', ascending=True))
    print(df4.columns)
    print("df4", df4, type(df4))
    df4 = df4.rename(columns={"uid":"uuid"})
    print("df4", df4)

    d = {"uuid":list,
         "time":list,
         "cid":list}
    df5 = df4.groupby("uid").agg(d)
    print(df5)

def test5_helper(arr):
    return list(arr)


if __name__ == '__main__':
    test5()

